var token=(new parent.window._user()).getData('token');
layui.define(["form","jquery"],function(exports){
	    var form = layui.form,
	    $ = layui.jquery,
	    MenuAdd = function(){};

    MenuAdd.prototype.provinces = function() {
        //加载厂家数据
        var proHtml = '',that = this;
        $.get('/dictionarygroup/vender?_token='+token, function (result) {
            var data=result.data;
            for (var i = 0; i < data.length; i++) {

                proHtml += '<option value="' + data[i].code + '">' + data[i].name + '</option>';
            }
            //初始化厂家数据
            $("select[name=company]").html(proHtml).removeAttr("disabled");
            form.render();
            if(data[0].childs){
                that.systems(data[0].childs);
                if(!$("input[name=companyCode]").val()){
                    $("input[name=companyCode]").val(data[0].code);
                }
            }
            form.on('select(company)', function (res) {
                $("input[name=companyCode]").val(res.value);
                var childs = data[$(this).index()].childs;
                if (childs) {
                    that.systems(childs);
                    $("input[name=productCode]").val(childs[0].code)
                } else {
                    $("select[name=product]").attr("disabled", "disabled");
                }
            });
        })
    }

    //加载系统数据
    MenuAdd.prototype.systems = function(systems) {
        var systemHtml = '',that = this;
        for (var i = 0; i < systems.length; i++) {
            systemHtml += '<option value="' + systems[i].code + '">' + systems[i].name + '</option>';
        }
        $("select[name=product]").html(systemHtml).removeAttr("disabled");

        if(systems[0].childs){
            if(!$("input[name=productCode]").val()){
                $("input[name=productCode]").val(systems[0].code)
            }

            $("input[name=productCode]").val(systems[0].code);
            that.versions(systems[0].childs);
        }

        form.render();
        form.on('select(product)', function (res) {
            $("input[name=productCode]").val(res.value);
            var childs = systems[$(this).index()].childs;
            if (childs) {
                that.versions(childs);
                $("input[name=modelNumber]").val(childs[0].code);
            } else {
                $("select[name=model]").attr("disabled", "disabled");
            }
        });
    }

    //加载版本数据
    MenuAdd.prototype.versions = function(versions) {
        var versionHtml = '';
        for (var i = 0; i < versions.length; i++) {
            versionHtml += '<option value="' + versions[i].code + '">' + versions[i].name + '</option>';
        }
        $("select[name=model]").html(versionHtml).removeAttr("disabled");
        form.render();
        if(versions.length>0){
            if (!$("input[name=modelNumber]").val()){
                $("input[name=modelNumber]").val(versions[0].code);
            }

        }
        form.on('select(model)', function (res) {

            $("input[name=modelNumber]").val(res.value);
        });
    }

    var MenuAdd = new MenuAdd();
    exports("threeselectadd",function(){
        MenuAdd.provinces();
    });
});
		